package com.songcha.library_business.helper.permission

import android.Manifest
import android.app.Activity
import android.app.Dialog
import android.content.Context
import android.content.pm.PackageManager
import android.location.LocationManager
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import com.songcha.library_common.helper.PermissionTipHelper
import com.songcha.library_common.helper.SharedPreferenceHelper
import com.tbruyelle.rxpermissions3.RxPermissions


class LocationPermissionHelper {
    companion object{
        private var mPermissions: RxPermissions?=null

        @JvmStatic
        fun <T> hasPermission(t:T,block:(Int)->Unit){
            var activity:Activity? = null
            if(t is Fragment){
                mPermissions = RxPermissions(t)
                activity = t.requireActivity()
            }else if(t is FragmentActivity){
                mPermissions = RxPermissions(t)
                activity = t
            }

            if(activity == null) return

            mPermissions?.shouldShowRequestPermissionRationale(activity,Manifest.permission.ACCESS_COARSE_LOCATION,Manifest.permission.ACCESS_FINE_LOCATION)
                ?.subscribe ({
                    val isLocationGranted = mPermissions?.isGranted(Manifest.permission.ACCESS_COARSE_LOCATION)?:false
                    val isFineLocationGranted = mPermissions?.isGranted(Manifest.permission.ACCESS_FINE_LOCATION)?:false
                    if(isLocationGranted && isFineLocationGranted){
                        block(PermissionResult.PERMISSION_GRANTED)
                    }else{
                        //single deny:true,forever deny:false,ask:false
                        if(it){
                            block(PermissionResult.PERMISSION_DENY)
                        }else{
                            if(SharedPreferenceHelper.getBoolean("permission","location")){
                                block(PermissionResult.PERMISSION_FOREVER_DENY)
                            }else{
                                block(PermissionResult.PERMISSION_ASK)
                            }

                        }
                    }

                },{

                })

        }

        @JvmStatic
        fun destroy(){
            mPermissions = null
        }

        @JvmStatic
        fun isGpsEnabled(context:Context):Boolean{
            val locationManager = context.getSystemService(Context.LOCATION_SERVICE) as LocationManager
            val isGpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)
            return isGpsEnabled
        }

        @JvmStatic
        fun <T> request(t:T,isRequestPermission:Boolean,onHasPermission:(isGranted:Boolean)->Unit,onNoPermission:()->Unit,onPermissionDeny:()->Unit, onGpsNotEnabled:()->Unit){
            var context:Context? = null

            if(t is Fragment)
                context = t.requireContext()
            else if(t is FragmentActivity)
                context = t
            if(context == null){
                return
            }

            //TODO
            /*if(!isGpsEnabled(context)){
                onGpsNotEnabled()
                return
            }*/

            hasPermission(t){pm->
                when(pm){
                    PermissionResult.PERMISSION_ASK ->{
                        if(isRequestPermission){
                            var dialogTip:Dialog?=null
                            if(context!=null){
                                dialogTip = PermissionTipHelper.showPermissionTip(context,"我们需要获取您的位置权限，用于展示天气信息")
                            }

                            //TODO
                            SharedPreferenceHelper.setBoolean("permission","location",true)

                            mPermissions?.request(Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION)
                                ?.subscribe {
                                    dialogTip?.dismiss()
                                    if (it) {
                                        onHasPermission(true)
                                    } else {
                                        onPermissionDeny()
                                    }
                                }
                        }else{
                            onNoPermission()
                        }
                    }
                    PermissionResult.PERMISSION_GRANTED ->{
                        onHasPermission(false)
                    }
                    PermissionResult.PERMISSION_DENY,PermissionResult.PERMISSION_FOREVER_DENY ->{
                        onPermissionDeny()
                    }
                }

            }

        }
    }
}